<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */
class FileController extends AdminController {

	/**
	 * 查询文件列表
	 * @author 尹雷
	 */
	public function fileList()
	{
		//获取文件类型参数
		$fileType = I('file_type', '', '/^\d+$/');
		if(!$fileType) {
			//默认列出首页动画图片
			$fileType = 1;
		}

		$where['status'] = array('egt', 0);
		$where['file_type'] = $fileType; 
		
		//查询文件列表
		$list = $this->lists('File', $where);
		
		if($list) {
			int_to_string($list);
			foreach($list as $key => &$val) {
				$val['operator_type_text'] = C('UPLOADER_TYPE')[$val['operator_type']];
				$val['size_format'] = round($val['size']/1024/1024, 2) . 'MB';
				$val['operator_name_type'] = C('UPLOADER_TYPE')[$val['operator_type']] . $val['operator_name'];
			}
		}

		unset($where['status']);
		$this->assign('fileType', C('APP_FILE')[0]);
		$this->assign('where', $where);
		$this->assign('_list', $list);
		$this->display('list');
	}
	
	/**
	 * 查询文件列表
	 * @author 尹雷
	 */
	public function fileList1()
	{
		//获取文件类型参数
		$fileType = I('file_type', '', '/^\d+$/');
		if(!$fileType) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		
		//组装参数
		$where['status'] = 1;
		$where['file_type'] = $fileType;
		$list = M('File')->where($where)->field('id,url,name,ext')->select();
		
		if(!$list) $this->ajaxReturn(array('error' => 1, 'info' => '查询失败'));
		
		$this->ajaxReturn(array('error' => 0, 'info' => $list));
	}
	
	/**
	 * 添加文件
	 * @author 尹雷
	 */
	public function addFile()
	{
		$fileType = I('file_type', '', '/^\d+$/');
		!$fileType && $this->error('参数错误');
		$this->assign('_title', '新增文件');
		$this->assign('fileLimit', file_limit($fileType));
		$this->display('add_edit');
	}
	
	/**
	 * 添加文件
	 * @author 尹雷
	 */
	public function editFile()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
		
		if(IS_GET) {
			$fileType = I('file_type', '', '/^\d+$/');
			!$fileType && $this->error('参数错误1');
			
			$file = D('File')->find($id);
			!$file && $this->error('文件查询失败');

			//判断是否是图片
			$ext = split(',', C('PICTURE_UPLOAD.exts'));
			if(in_array($file['ext'], $ext)) $file['picture'] = true;
			$file['size_format'] = round($file['size']/1024/1024, 2) . 'MB';
			
			$this->assign('file', $file);
			$this->assign('_title', '编辑文件');
			$this->assign('buttonText', '更新文件...');
			$this->assign('fileLimit', file_limit($fileType));
			$this->display('add_edit');
		}
		
		$file = D('File');
		if($file->create()) {
			$res = $file->save();
			if(!$res) $this->ajaxReturn(array('error'=>1, 'msg'=>'更新失败'));
			$this->ajaxReturn(array('error'=>0, 'msg'=>'更新成功'));
		}
		$this->ajaxReturn(array('error'=>1, 'msg'=>'数据对象创建失败'));
	}
	
	/**
	 * TODO:禁用的业务逻辑待定
	 * 更新基金状态
	 * @author 尹雷
	 */
	public function changeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('File', $map );
				break;
			case 'resume':
				$this->resume('File', $map );
				break;
			case 'delete':
				$this->delete('File', $map );
				break;
			default:
				$this->error('参数非法');
		}
	
	}
	
    /**
     * 文件上传，后台管理员上传
     * @author 尹雷
     */
    public function upload(){
    	//获取文件类型参数
    	$fileType = I('file_type', '', '/^\d+$/');
    	if(!$fileType) $this->ajaxReturn(array('error' => 1, 'msg' => '文件类型参数错误'));
		$param['fileType'] = I('file_type', '', '/^\d+$/');
		
		//获取文件上传都信息
		$param['operatorId'] = session('user_auth')['uid'];
		$param['operatorName'] = session('user_auth')['username'];
		$param['operatorType'] = 2;
		
		//判断是否是编辑
		if(I('id', '', '/^\d+$/')) {
			$param['id'] = I('id', '', '/^\d+$/');
		}
		
		//是否需要修改name参数
		if(I('name', '')) {
			$param['name'] = I('name');
		}
		
		//实例化上传文件模型对象
		$file = D('File');
		$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
		
		//设置允许上传的文件类型
		$set = C('DOWNLOAD_UPLOAD');
		$set['exts'] = C('FILE_EXTS')[$param['fileType']];
		C('DOWNLOAD_UPLOAD', $set);
		
		//上传文件
		$info = $file->upload($_FILES, C('DOWNLOAD_UPLOAD'), $param, C('DOWNLOAD_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));
		
		$return  = array('error' => 0, 'msg' => '上传成功');
		if(!$info) {
			$return['error'] = 1;
			$return['msg']   = '上传失败';
		}
		
        //返回JSON数据
        $this->ajaxReturn($return);
    }

    /**
     * 查询文件详情
     * @author 尹雷
     */
    public function detailFile()
    {
    	$id = I('id', '', '/^\d+$/');
	    !$id && $this->error('参数错误');
	    $where['id'] = $id;
	    
    	//查询文件列表
    	$file = $this->lists('File', $where);
    	!$file && $this->error('查询失败');
    	int_to_string($file);
    	$file = $file[0];
    	
    	$file['file_type_text'] = C('APP_FILE')[$file['file_type']]['name'];
    	$file['size_format'] = round($file['size']/1024/1024, 2) . 'MB';
    	
    	//判断是否是图片
    	$ext = split(',', C('PICTURE_UPLOAD.exts'));
    	if(in_array($file['ext'], $ext)) $file['picture'] = true;
    	
    	$this->assign('file', $file);
    	$this->display('detail');
    }
    
    /* 下载文件 */
    public function download($id = null){
        if(empty($id) || !is_numeric($id)){
            $this->error('参数错误！');
        }

        $logic = D('Download', 'Logic');
        if(!$logic->download($id)){
            $this->error($logic->getError());
        }

    }

    /**
     * 前端通过kinder插件上传文件
     * @author 尹雷
     */
    public function kinderUpload() 
    {
    	//文件类型参数
    	$fileType = 80;
    	$param['fileType'] = $fileType;
    	
    	//获取文件上传都信息
    	$param['operatorId'] = session('user_auth')['uid'];
    	$param['operatorName'] = session('user_auth')['username'];
    	$param['operatorType'] = 2;
    	
    	//实例化上传文件模型对象
    	$file = D('File');
    	$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
    	
    	//设置允许上传的文件类型
    	$set = C('DOWNLOAD_UPLOAD');
    	$set['exts'] = C('FILE_EXTS')[$param['fileType']];
    	C('DOWNLOAD_UPLOAD', $set);
    	
    	//上传文件
    	$info = $file->upload($_FILES, C('DOWNLOAD_UPLOAD'), $param, C('DOWNLOAD_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));
    	
    	$return = array();
    	if($info) $this->ajaxReturn(array('error' => 0, 'url' => C('FUND_DOMAIN1') . $info['imgFile']['url']));
    	$this->ajaxReturn(array('error' => 1, 'message' => '上传失败'));
    }
   
}
